﻿#!/usr/bin/env python
# ------------------------------------------------------------------------------
#
# Main module, from where to run the sim
#
# Copyright (c) 2012 Fernando Garcia Huerta
# nanomuelle@gmail.com
#
#    This file is part of SimCell
#
#    SimCell is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    SimCell is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with SimAnim.  If not, see <http://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------------------

__author__    = "$Author$"
__revision__  = "$Revision$"
__date__      = "$Date: 16 Jan 2012 $"
__copyright__ = "2012 nanomuelle.com"
__contact__   = "http://blog.nanomuelle.com"

import os
import sys
import math
# import random
# import pprint

import pygame
from pygame.locals import *

from config import * #SCREENRECT, SCREENSCALE

# from resource_manager import rm

# from simcell.world.world import World

from simcell.cell.cell import Cell

def main(argv=None):
    os.environ["SDL_VIDEO_CENTERED"] = "1"

    cells = []
    for i in range(10):
        cells.append(Cell.generateRandomCell(200, 200))
        
    for cell in cells:
        print cell.__repr__()
        
    pygame.init()
    
    winstyle = 0  # |FULLSCREEN
    bestdepth = pygame.display.mode_ok((SCREENRECT.width * SCREENSCALE, 
                                        SCREENRECT.height * SCREENSCALE), winstyle, 32)
    
    screen = pygame.display.set_mode((SCREENRECT.width * SCREENSCALE,
                                      SCREENRECT.height * SCREENSCALE), winstyle, bestdepth)
    pygame.mouse.set_visible(0)
    
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((5, 5, 5))
    center = (int(background.get_width()/2), int(background.get_height()/2))
    
    colors = {'H':(120,0,0), 'V':(0,128,0), '-':(0,0,128)}
    
    for cell in cells:    
        for protein, num in cell.cytoplasm.cytosol.proteins.bag.itervalues():
        
            screen.blit(background, (0, 0))
        
            if pygame.font:
                font = pygame.font.Font(None, 20)
                info_cell = "Cell %d" % cell.id
                text_cell = font.render(info_cell, 1, (255, 255, 10))
                
                info_protein = "Protein: %s" % "".join(map(str, protein.strand))
                text_protein = font.render(info_protein, 1, (255, 255, 10))
                
                screen.blit(text_cell, (0,0))
                screen.blit(text_protein, (0,16))
            
            angle = 0
            fi = 0
            psi = 0
            d = 20
            r = int(d/2)
            pos = center
            poss = [pos]
            for amino in protein.strand:
                color = colors[amino.name]
                angle += amino.fi
                
                ix = d * math.cos(angle)
                iy = d * math.sin(angle)

                pos = (int(pos[0] + ix), int(pos[1] + iy))
                poss.append(pos)

                pygame.draw.circle(screen, color, pos, r)
                
                angle += amino.psi
                
            color = (255,255,255)
            if len(poss) > 1:
                rect = pygame.draw.lines(screen, color, False, poss, 1)
                print rect
                pygame.draw.rect(screen, color, rect, 1)
            # for organism in w.population:
              # pos = organism.pos
              # for cell in organism.cells:
                # size = cell.proteinBag.getVolume() / 1000
                # pygame.draw.circle(screen, (128, 128, 255), (int(pos[0]), int(pos[1])), size)
                # pos = (pos[0] + size, pos[1])

            pygame.display.flip()
                
            while (pygame.event.wait().type != KEYDOWN): pass
    
    
if __name__ == "__main__":
    sys.exit(main())
